package com.htfyun.eink.pw.gson;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class JsonUtil {

    @Nullable
    public static <T> T jsonToBean(String json, Class<T> clazz) {
        try {
            return GsonUtils.fromJson(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @NonNull
    public static <T> List<T> jsonToList(String json, Class<T> clazz) {
        List<T> list = null;
        try {
            list = GsonUtils.fromJson(json, GsonUtils.getListType(clazz));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;

    }

    @NonNull
    public static String beanToJson(Object object) {
        if (object == null) {
            return "";
        }
        try {
            return GsonUtils.toJson(object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
